﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using MVC4Shop.Models;

namespace MVC4Shop.dao
{
    public class ColorDAO
    {
        public static List<ColorModel> GetAll()
        {
            return Convert( DataUtil.executeStore("sp_Color_GetAll", null));
        }

        public static Boolean Delete(String id)
        {
            SqlParameter[] param = new SqlParameter[1];
            int i = 0;
            param[i] = new SqlParameter("@id", SqlDbType.NVarChar);
            param[i++].Value = id;
            return DataUtil.executeNonStore("sp_Color_Delete", param);
        }

        public static Boolean Edit(ColorModel model)
        {
            SqlParameter[] param = new SqlParameter[2];
            int i = 0;
            param[i] = new SqlParameter("@id", SqlDbType.NVarChar);
            param[i++].Value = model.id;
            param[i] = new SqlParameter("@name", SqlDbType.NVarChar);
            param[i++].Value = model.name;
            return DataUtil.executeNonStore("sp_Color_Edit", param);
        }

        public static Boolean Insert(ColorModel _model)
        {
            SqlParameter[] param = new SqlParameter[2];
            int i = 0;
            param[i] = new SqlParameter("@name", SqlDbType.NVarChar);
            param[i++].Value = _model.name;
            param[i] = new SqlParameter("@image", SqlDbType.NVarChar);
            param[i++].Value = _model.image;
            return DataUtil.executeNonStore("sp_Color_Insert", param);
        }

        public static List<ColorModel> Convert(DataTable dt)
        {
            List<ColorModel> list = new List<ColorModel>();
            ColorModel model;
            foreach (DataRow row in dt.Rows)
            {
                model = new ColorModel();
                model.id = row["color_id"].ToString();
                model.name = row["name"].ToString();
                list.Add(model);
            }
            return list;
        }
    }
}